package spiralmatrixii

// import "fmt"

// 生成螺旋矩阵
func generateMatrix(n int) [][]int{
	if n==0{
		return [][]int{}
	}
	if n== 1{
		return [][]int{{1}}
	}
	matrix := make([][]int,n)
	for i:=range matrix {
		matrix[i] = make([]int, n)
	}

	num := 1  //数字从1开始
	left,right,top,bottom := 0,n-1,0,n-1
	for num<=n*n {
		for i := left; i <= right; i++ {
            matrix[top][i] = num
            num++
        }
        top++
        for i := top; i <= bottom; i++ {
            matrix[i][right] = num
            num++
        }
        right--
        for i := right; i >= left; i-- {
            matrix[bottom][i] = num
            num++
        }
        bottom--
        for i := bottom; i >= top; i-- {
            matrix[i][left] = num
            num++
        }
        left++
	}
	return matrix
}
